import Api from '@/api/common';
import ManApi from '@/api/manage';
import TrainData from '@/data/train';
import ProductData from '@/data/product';

export default {
  state: {
    // 员工
    empSelect: [],
    empSelectAll: [],
    // 角色
    roleSelect: [],
    roleSelectAll: [],
    // 仓库
    storeSelect: [],
    storeSelectAll: [],
    // 部门
    oneDeptSelect: [],
    oneDeptSelectAll: [],
    // 仓管员
    keeperSelect: [],
    keeperSelectAll: [],
    // 供应商
    supplySelect: [],
    supplySelectAll: [],
    // 物料
    materialList: [],
    materialSelect: [],
    // 国家
    nationSelect: [],
    nationSelectAll: [],
    // 城市
    countrySelect: [],
    countrySelectAll: [],
    // 供应商性质
    vendorChatacterSelect: [],
    vendorChatacterSelectAll: [],
    // 供应商类别
    vendorCateSelect: [],
    vendorCateSelectAll: [],
    // 供应商分级
    vendorGradeSelect: [],
    vendorGradeSelectAll: [],
    // 币种类别
    currencySelect: [],
    currencySelectAll: [],
    // 默认税率
    defaultTaxRateSelect: [],
    defaultTaxRateSelectAll: [],
    // 行业
    industrySelect: [],
    industrySelectAll: [],
    // 发票类型
    invoiceTypeSelect: [],
    invoiceTypeSelectAll: [],
    // 付款条件
    paymentSelect: [],
    paymentSelectAll: [],
    // 职工职位
    comPositionSelect: [],
    comPositionSelectAll: [],
    // 供应商职位
    positionSelect: [],
    positionSelectAll: [],
    // 规模
    scaleSelect: [],
    scaleSelectAll: [],
    // 汇款方式
    settleSelect: [],
    settleSelectAll: [],
    // 单位
    unitSelect: [],
    unitSelectAll: [],
    // 制造商
    skuManSelect: [],
    skuManSelectAll: [],
    // 物料分级
    skuLevelSelect: [],
    skuLevelSelectAll: [],
    // 物料属性
    skuPropSelect: [],
    skuPropSelectAll: [],
    // 物料类型
    skuTypeSelect: [],
    skuTypeSelectAll: [],
    // 课程类别
    crsTypeSelect: [],
    crsTypeSelectAll: [],
    // 课程形式
    crsFormSelect: [],
    crsFormSelectAll: [],
    // 培训机构
    agencySelect: [],
    agencySelectAll: [],
    // 培训教师
    teacherSelect: [],
    teacherSelectAll: [],
    // 培训指派
    assignerSelect: [],
    assignerSelectAll: [],
    // 产品列表
    productSelect: [],
    productSelectAll: [],
    // 产品节点列表
    productLevelSelect: [],
    productLevelSelectAll: []
  },
  mutations: {
    SET_EMP_SELECT (state, select) {
      state.empSelect = select;
      state.empSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_ROLE_SELECT (state, select) {
      state.roleSelect = select;
      state.roleSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_STORE_SELECT (state, select) {
      state.storeSelect = select;
      state.storeSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_ONE_DEPT_SELECT (state, select) {
      state.oneDeptSelect = select;
      state.oneDeptSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_KEEPER_SELECT (state, select) {
      state.keeperSelect = select;
      state.keeperSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_SUPPLY_SELECT (state, select) {
      state.supplySelect = select;
      state.supplySelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_MATERIAL_LIST (state, list) {
      state.materialList = list;
    },
    SET_MATERIAL_SELECT (state, select) {
      state.materialSelect = select;
    },
    // ******************供应商************************
    SET_NATION_SELECT (state, select) {
      // 国家
      state.nationSelect = select;
      state.nationSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_CITY_SELECT (state, select) {
      // 城市
      state.countrySelect = select;
      state.countrySelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_CHATACTER_SELECT (state, select) {
      // 供应商性质
      state.vendorChatacterSelect = select;
      state.vendorChatacterSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_CATE_SELECT (state, select) {
      // 供应商类别
      state.vendorCateSelect = select;
      state.vendorCateSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_GRADE_SELECT (state, select) {
      // 供应商分级
      state.vendorGradeSelect = select;
      state.vendorGradeSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_CURRENCY_SELECT (state, select) {
      // 币种类别
      state.currencySelect = select;
      state.currencySelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_DEFAULT_TAX_RATE_SELECT (state, select) {
      // 默认税率
      state.defaultTaxRateSelect = select;
      state.defaultTaxRateSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_INDUSTRY_SELECT (state, select) {
      // 行业
      state.industrySelect = select;
      state.industrySelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_INVOICE_SELECT (state, select) {
      // 发票类型
      state.invoiceTypeSelect = select;
      state.invoiceTypeSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_PAYMENT_SELECT (state, select) {
      // 付款条件
      state.paymentSelect = select;
      state.paymentSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_COM_POSITION_SELECT (state, select) {
      // 职工职位
      state.comPositionSelect = select;
      state.comPositionSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_POSITION_SELECT (state, select) {
      // 供应商职位
      state.positionSelect = select;
      state.positionSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_SCALE_SELECT (state, select) {
      // 规模
      state.scaleSelect = select;
      state.scaleSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_SETTLE_SELECT (state, select) {
      // 汇款方式
      state.settleSelect = select;
      state.settleSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    // ******************物料************************
    SET_UNIT_SELECT (state, select) {
      // 单位
      state.unitSelect = select;
      state.unitSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_SKU_MAN_SELECT (state, select) {
      // 制造商
      state.skuManSelect = select;
      state.skuManSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_SKU_LEVEL_SELECT (state, select) {
      // 物料分级
      state.skuLevelSelect = select;
      state.skuLevelSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_SKU_PROP_SELECT (state, select) {
      // 物料属性
      state.skuPropSelect = select;
      state.skuPropSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_SKU_TYPE_SELECT (state, select) {
      // 物料类型
      state.skuTypeSelect = select;
      state.skuTypeSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    // ******************培训************************
    SET_CRS_TYPE_SELECT (state, select) {
      // 课程类别
      state.crsTypeSelect = select;
      state.crsTypeSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_CRS_FORM_SELECT (state, select) {
      // 课程形式
      state.crsFormSelect = select;
      state.crsFormSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_AGENCY_SELECT (state, select) {
      // 物料类型
      state.agencySelect = select;
      state.agencySelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_TEACHER_SELECT (state, select) {
      // 物料类型
      state.teacherSelect = select;
      state.teacherSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_ASSIGNER_SELECT (state, select) {
      // 物料类型
      state.assignerSelect = select;
      state.assignerSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_PRODUCT_SELECT (state, select) {
      state.productSelect = select;
      state.productSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_PRODUCT_LEVEL_SELECT (state, select) {
      state.productLevelSelect = select;
      state.productLevelSelectAll = [{ name: '全部', value: null }].concat(select);
    },
    SET_PRODUCT_MODULE_SELECT (state, select) {
      state.productModuleSelect = select;
      state.productModuleSelectAll = [{ name: '全部', value: null }].concat(select);
    },
  },
  actions: {
    setEmpSelect ({ commit }) {
      return Api.getEmployees().then((empSelect) => {
        commit('SET_EMP_SELECT', empSelect);
        // TODO
        // 暂时在此初始化培训公共接口
        commit('SET_CRS_TYPE_SELECT', TrainData.crsTypeSelect);
        commit('SET_CRS_FORM_SELECT', TrainData.crsFormSelect);
        commit('SET_AGENCY_SELECT', TrainData.agencySelect);
        commit('SET_TEACHER_SELECT', TrainData.teacherSelect);
        commit('SET_ASSIGNER_SELECT', TrainData.assignerSelect);
        commit('SET_PRODUCT_SELECT', ProductData.productSelect);
        commit('SET_PRODUCT_LEVEL_SELECT', ProductData.levelSelect);
        commit('SET_PRODUCT_MODULE_SELECT', ProductData.moduleSelect);
        return empSelect;
      }).catch(error => Promise.reject(error));
    },
    setRoleSelect ({ commit }) {
      // 取得角色列表
      return ManApi.getRoleList().then((result) => {
        const roleList = result.obj;
        const roleSelect = roleList.map(role => ({
          name: role.roleName,
          value: role.id
        }));
        commit('SET_ROLE_SELECT', roleSelect);
        return roleSelect;
      }).catch(error => Promise.reject(error));
    },
    setStoreSelect ({ commit }) {
      return Api.getStores().then((storeSlect) => {
        commit('SET_STORE_SELECT', storeSlect);
        return storeSlect;
      }).catch(error => Promise.reject(error));
    },
    setOneDeptSelect ({ commit }) {
      return Api.getOneLevelDept().then((oneDeptSelect) => {
        commit('SET_ONE_DEPT_SELECT', oneDeptSelect);
        return oneDeptSelect;
      }).catch(error => Promise.reject(error));
    },
    setKeeperSelect ({ commit }) {
      return Api.getStoreKeepers().then((keeperSelect) => {
        commit('SET_KEEPER_SELECT', keeperSelect);
        return keeperSelect;
      }).catch(error => Promise.reject(error));
    },
    setSupplySelect ({ commit }) {
      return Api.getSupplys().then((supplySelect) => {
        commit('SET_SUPPLY_SELECT', supplySelect);
        return supplySelect;
      }).catch(error => Promise.reject(error));
    },
    setMaterialSelect ({ commit }) {
      return Api.getMaterials().then((materialList) => {
        const materialSelect = materialList.map(item => ({
          value: item.skuId,
          name: item.skuName
        }));
        commit('SET_MATERIAL_LIST', materialList);
        commit('SET_MATERIAL_SELECT', materialSelect);
        return materialList;
      }).catch(error => Promise.reject(error));
    },
    setVendorBasicSelect ({ commit }) {
      return Api.getVendorBasicConfigs().then((config) => {
        commit('SET_NATION_SELECT', config.countrys);
        commit('SET_CITY_SELECT', config.citys);
        commit('SET_CHATACTER_SELECT', config.characters);
        commit('SET_CATE_SELECT', config.vendorCategory);
        commit('SET_GRADE_SELECT', config.vendorGrade);
        commit('SET_SCALE_SELECT', config.scales);
        commit('SET_INDUSTRY_SELECT', config.industrys);
        commit('SET_POSITION_SELECT', config.positions);
        commit('SET_COM_POSITION_SELECT', config.comPositions);
        commit('SET_CURRENCY_SELECT', config.currencys);
        commit('SET_SETTLE_SELECT', config.settlements);
        commit('SET_PAYMENT_SELECT', config.paymentTerms);
        commit('SET_INVOICE_SELECT', config.invoiceTypes);
        commit('SET_DEFAULT_TAX_RATE_SELECT', config.defaultTaxRates);
        return config;
      }).catch(error => Promise.reject(error));
    },
    setMateBasicSelect ({ commit }) {
      return Api.getBasicConfigs().then((config) => {
        commit('SET_UNIT_SELECT', config.basicUnits);
        commit('SET_SKU_MAN_SELECT', config.manufacturers);
        commit('SET_SKU_LEVEL_SELECT', config.skuLevels);
        commit('SET_SKU_PROP_SELECT', config.skuPropertys);
        commit('SET_SKU_TYPE_SELECT', config.skuTypes);
        return config;
      }).catch(error => Promise.reject(error));
    }
  }
};
